package com.yanyilu.qiubite.service;

import com.yanyilu.qiubite.entity.Code;
import com.yanyilu.qiubite.jpa.CodeJpa;
import com.yanyilu.qiubite.util.SendMessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CodeService {
    @Autowired
    CodeJpa codeJpa;

    /**
     * 发送验证码
     * @param phoneNumber
     * @return
     */
    public boolean  sendCode(String phoneNumber) {
        Code code = codeJpa.findByPhoneNumber(phoneNumber);
        Code codeRes = SendMessageUtil.sendMessage(phoneNumber);

        if(null != codeRes.getCode()){
            if(null != code){
                codeRes.setCodeId(code.getCodeId());
            }
            codeJpa.save(codeRes);
            return true;
        }else{
            return false;
        }
    }
}
